package Boke;
class Counter {
    // 用来保存计数的变量
    public int count=0;

     public void increase() {
         synchronized (this){
             count++;
         }
    }

}
public class SameUpdate2 {



        // 这个实例用来进行累加.
        // public static Counter counter = new Counter();

        public static void main(String[] args) {
            Counter counter = new Counter();

            Thread t1 = new Thread(() -> {
                for (int i = 0; i < 10000; i++) {
                    counter.increase();
                }
            });
            Thread t2 = new Thread(() -> {
                for (int i = 0; i < 10000; i++) {
                    counter.increase();
                }
            });
            t1.start();
            t2.start();

            try {
                t1.join();
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("count: " + counter.count);
        }
}
